from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import template
from DataClass.FullPage import FullPage
from DataClass.WebsiteRec import WebsiteRec

import os
import cgi

class FullPageHandler(webapp.RequestHandler):
  def get(self):

    querystring=cgi.FieldStorage()
    pagename=querystring.getvalue('pagename')
    if not pagename or (pagename == ""):
        pagename="main"
        
    Website = WebsiteRec.GetCurrentWebsite(self.request)
    Page = None

    if Website:
        fullpage_query = FullPage.all().filter('pagename =', pagename).filter('sitename =', Website.sitename)
        pagecontentarr = fullpage_query.fetch(1)
        if pagecontentarr:
            Page = pagecontentarr[0]

    if Page:
        template_values = {
          'pagecontent': Page.contenttext
          }
    else:
        template_values = {
          'pagecontent': '<div>Page not found</div>'
          }
    
    path = os.path.join(os.path.dirname(__file__), 'page.html')
    self.response.out.write(template.render(path, template_values))
